package github.fafaovo.common.event;

import github.fafaovo.common.gui.ExampleHUD;
import github.fafaovo.common.mycapability.ISpeedUpCapability;
import github.fafaovo.core.init.ItemInit;
import github.fafaovo.core.registry.CommonSetupEventHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;


@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class ClientRenderEvent {
    @SubscribeEvent
    public static void onRenderGameOverlayPost(RenderGameOverlayEvent event) {
        //没有渲染整个游戏之前不要渲染我们自己的东西
        if (!RenderGameOverlayEvent.ElementType.ALL.equals(event.getType())) {
            return;
        }
        Minecraft minecraft = Minecraft.getInstance();
        //从MC中拿到实体
        Entity renderViewEntity = minecraft.getRenderViewEntity();
        if (renderViewEntity instanceof PlayerEntity) {
            PlayerEntity playerEntity = (PlayerEntity) renderViewEntity;

            if (playerEntity.inventory.hasItemStack(new ItemStack(ItemInit.ENERGY_METER.get()))) {
                LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
                capability.ifPresent((c) -> {
                    float level = c.getLevel();
                    ExampleHUD exampleHUD = new ExampleHUD(event.getMatrixStack());
                    exampleHUD.render(level);
                    //虽然绘制出来了，但是还是在客户端上，与服务器端还没同步数据，所以说我们需要同步数据
                });
            }

        }
    }


}
